#include "ModuleData.h"

#include <iostream>
#include "ChannelData.h"
//This function gets the information we need about the module
ModuleData::ModuleData(string name, bool _isChannel)
{
	mName = name;
	char id[11];
	itoa(getNextUID(), id, 10);
	mUID = id;
	mIsChannel = _isChannel;
}
//destructor
ModuleData::~ModuleData()
{
	for (list<ModuleData*>::iterator i = mModuleDataList.begin(); i != mModuleDataList.end(); i++) delete (*i);
	for (list<ProcessData*>::iterator j = mProcessList.begin(); j != mProcessList.end(); j++) delete (*j);
	for (list<PortData*>::iterator k = mPortDataList.begin(); k != mPortDataList.end(); k++) delete (*k);
	for (list<ChannelData*>::iterator l = mChannelDataList.begin(); l != mChannelDataList.end(); l++) delete (*l);
}
//This function adds a process
void ModuleData::addProcess(ProcessData* process)
{
	mProcessList.push_front(process);
}
//This fuction adds the channel data which we have in channeldata.h
void ModuleData::addChannelData(ChannelData* channelData)
{
	mChannelDataList.push_front(channelData);
}
//This fuction adds the module data which we have in moduledata.h	
void ModuleData::addModuleData(ModuleData* moduleData)
{
	mModuleDataList.push_front(moduleData);
}
//This function returns a module data list
list<ModuleData*>* ModuleData::getModuleDataList()
{
	return &mModuleDataList;
}
//This function returns a channnel data list
list<ChannelData*>* ModuleData::getChannelDataList()
{
	return &mChannelDataList;
}
//This function returns a process list
list<ProcessData*>* ModuleData::getProcessList()
{
	return &mProcessList;
}
//This function returns a module name
string ModuleData::getName()
{
	return mName;
}
//This function returns a true or false to see if it is a channel or not.
bool ModuleData::isChannel()
{
	return mIsChannel;
}

void ModuleData::debug(int level)
{
	cout.write(" ", level);
	cout << mName << endl;
	//iterates through the channel data list to get the source name and type and to get the target name and type
	for (list<ChannelData*>::iterator i = mChannelDataList.begin(); i != mChannelDataList.end(); i++)
	{
		cout.write(" ", level+1);
		cout << (*i)->getSourceName() << ":" << (*i)->getSourceType() << " -> " << (*i)->getTargetName() << ":" << (*i)->getTargetType() << endl;
	}
	//iterates through the process list to get the process name and type
	for (list<ProcessData*>::iterator j = mProcessList.begin(); j != mProcessList.end(); j++)
	{
		cout.write(" ", level+1);
		cout << (*j)->getName() << ":" << (*j)->getType() << endl;
	}
	//iterates through the module data using a recursive function
	for (list<ModuleData*>::iterator k = mModuleDataList.begin(); k != mModuleDataList.end(); k++)
	{
		(*k)->debug(level+1);
	}
}
//adds more port data on to port data list
void ModuleData::addPortData(PortData* portData)
{
	mPortDataList.push_back(portData);
}
// Returns the full list of port data
list<PortData*>* ModuleData::getPortDataList()
{
	return &mPortDataList;
}
//returns the ID of the process
string ModuleData::getUID()
{
	return mUID;
}
//returns the ID of the next process
int ModuleData::getNextUID()
{
	return mNextUID++;
}

int ModuleData::mNextUID = 0;